package com.share.dubbo.es.manager.impl;

import com.share.common.common.Page;
import com.share.dubbo.es.manager.EsAccessLogManager;
import com.share.dubbo.es.model.EsAccessLogReq;
import com.share.dubbo.es.model.EsAccessLogResq;
import com.share.dubbo.es.model.EsAccessLogSaveReq;
import com.share.es.model.enums.EsEnum;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/10/27 19:05
 */
@Slf4j
@Service
public class EsAccessLogManagerImpl implements EsAccessLogManager {
    private EsEnum esEnum = EsEnum.ACCESS_LOG;
    @Autowired
    private TransportClient transportClient;

    /*需求总量统计*/
    @Override
    public Long count(EsAccessLogReq req) {
        /*设置索引库和文档*/
        SearchRequest searchRequest = new SearchRequest(esEnum.getIndex());
        searchRequest.searchType(esEnum.getType());

        /*构建查询条件*/
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        if (StringUtils.isEmpty(req.getAreaCode())) {
            sourceBuilder.query(QueryBuilders.termQuery("areaCode", req.getAreaCode()));//等值条件设定
        }

        if (StringUtils.isEmpty(req.getAreaType())) {
            sourceBuilder.query(QueryBuilders.termQuery("areaType", req.getAreaType()));//等值条件设定
        }

        if (StringUtils.isEmpty(req.getTag())) {
            sourceBuilder.query(QueryBuilders.termQuery("tag", req.getAreaCode()));//等值条件设定
        }

        AbstractAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("count").field("name");
        sourceBuilder.aggregations().addAggregator(valueCountAggregationBuilder); /*count计数*/

        log.info("普通查询的DSL语句:" + sourceBuilder.toString());
        searchRequest.source(sourceBuilder);

        GetRequestBuilder getRequestBuilder = transportClient.prepareGet();//按照id取
        DeleteRequestBuilder deleteRequestBuilder = transportClient.prepareDelete();//按照id删除
        IndicesAdminClient indices = transportClient.admin().indices();
        CreateIndexRequest reqeu = new CreateIndexRequest();

        indices.create(reqeu);

        GetResponse getFields = getRequestBuilder.get();

        //执行查询
        SearchResponse searchResponse = transportClient.search(searchRequest).actionGet();
        SearchHits hits = searchResponse.getHits();
        Long totalHits = hits.getTotalHits().value;
        return totalHits;
    }

    @Override
    public Page<EsAccessLogResq> pageList(EsAccessLogReq req) {
        return null;
    }

    @Override
    public Long save(EsAccessLogSaveReq req) {
        return 1L;
    }
}
